package com.dingreading.cloud.admin.util;


import com.dingreading.cloud.admin.dto.ShopCartPriceDto;
import com.dingreading.cloud.admin.entity.Goods;
import com.dingreading.cloud.admin.entity.GoodsType;
import com.dingreading.cloud.admin.entity.ShopCart;
import com.dingreading.cloud.common.util.DoubleUtil;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.NepUtil;

public class ShopCartUtilV2 {

    /**
     * 新增正常商品的购物车记录
     */
    public static ShopCart newShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType, addUid, addName,
                freeDeliveryStatus, storeGoodsDiscountRate
        );

        data.setPaymentMethod(goods.getPaymentMethod());
        data.setTotalWeight(DoubleUtil.mul(buyNum, NepUtil.nullToZero(goodsType.getWeight())));
        return data;
    }

    /**
     * 新增组合商品的购物车记录
     */
    public static ShopCart newMainComboShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            Integer paymentMethod, Double totalWeight,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType, addUid, addName,
                freeDeliveryStatus, storeGoodsDiscountRate
        );

        data.setPaymentMethod(paymentMethod);
        data.setTotalWeight(totalWeight);
        return data;
    }

    /**
     * 新增组合子商品的购物车记录
     */
    public static ShopCart newChildComboShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer mainComboType, String uid, Integer buyNum, String addUid, String addName,
            String mainShopCartUid, Long mainGoodsId, String mainGoodsName, Long mainGoodsTypeId, String mainGoodsTypeName,
            Integer freeDeliveryStatus
    ) {
        ShopCart data = newBaseShopCart(
                goods, goodsType, corpUid, storeUid, staffUid, type, buyNum, uid, mainComboType,
                addUid, addName, freeDeliveryStatus, null
        );

        data.setPaymentMethod(goods.getPaymentMethod());
        double weight = NepUtil.nullToZero(goodsType.getWeight());
        data.setWeight(weight);
        data.setTotalWeight(DoubleUtil.mul(buyNum, weight));
        data.setMainShopCartUid(mainShopCartUid);
        data.setMainGoodsId(mainGoodsId);
        data.setMainGoodsTypeId(mainGoodsTypeId);
        data.setMainGoodsName(mainGoodsName);
        data.setMainGoodsTypeName(mainGoodsTypeName);
        return data;
    }

    /**
     * 新增购物车信息
     */
    private static ShopCart newBaseShopCart(
            Goods goods, GoodsType goodsType, String corpUid, String storeUid, String staffUid,
            Integer type, Integer buyNum, String uid, Integer mainComboType, String addUid, String addName,
            Integer freeDeliveryStatus, Double storeGoodsDiscountRate
    ) {
        ShopCart data = new ShopCart();
        data.setType(type);
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStaffUid(staffUid);
        data.setGoodsId(goods.getId());
        data.setGoodsName(goods.getName());
        data.setImg(goodsType.getImg());
        data.setGoodsTypeId(goodsType.getId());
        data.setGoodsTypeName(goodsType.getName());
        data.setRealStatus(goods.getRealStatus());
        data.setUnit(goodsType.getUnit());
        data.setMinBuyNum(goods.getMinBuyNum());
        data.setBuyNum(buyNum);

        double unitPrice = goodsType.getPrice();
        // 计算购物车商品的价格（学科折扣）
//        ShopCartPriceDto priceDto = getDiscountRateUnitPrice(goodsType.getDiscountStatus(), storeGoodsDiscountRate, unitPrice);
//        double discountRateUnitPrice = priceDto.getUnitPrice();
//        data.setOriginalUnitPrice(unitPrice);
//        data.setUnitPrice(discountRateUnitPrice);
//        data.setPrice(DoubleUtil.mul(buyNum, discountRateUnitPrice));
//        data.setDiscountRate(priceDto.getDiscountRate());
//        data.setRemark(priceDto.getRemark());
        data.setOriginalUnitPrice(unitPrice);
        data.setOriginalPrice(DoubleUtil.mul(buyNum, unitPrice));
        data.setUnitPrice(unitPrice);
        data.setPrice(data.getOriginalPrice());

        data.setCouponStatus(goods.getCouponStatus());
        data.setStatus(EnumType.ShopCart.WAIT_CONFIRM.getType());
        data.setStatusName(EnumType.ShopCart.WAIT_CONFIRM.getName());
        data.setStockOutMethod(goodsType.getStockOutMethod());
        data.setAddStaffUid(addUid);
        data.setAddStaffName(addName);
        data.setLastStaffUid(addUid);
        data.setLastStaffName(addName);
        data.setUid(uid);
        data.setComboType(goodsType.getComboType());
        data.setMainComboType(mainComboType);
        data.setWeight(NepUtil.nullToZero(goodsType.getWeight()));

        if (freeDeliveryStatus == null)
            data.setFreeDeliveryStatus(0);
        else
            data.setFreeDeliveryStatus(freeDeliveryStatus);
        return data;
    }

    /**
     * 计算购物车商品的价格（学科折扣）
     */
    public static ShopCartPriceDto getDiscountRateUnitPrice(
            Integer discountStatus, Double discountRate, Double unitPrice
    ) {
        ShopCartPriceDto priceDto = new ShopCartPriceDto();
        // 判断是否符合门店学科的折扣规则
        if (discountStatus != null && discountStatus.equals(1) && discountRate != null && discountRate > 0) {
            double discountRateUnitPrice = DoubleUtil.mul(unitPrice, discountRate);
            priceDto.setUnitPrice(discountRateUnitPrice);
            priceDto.setOriginalUnitPrice(unitPrice);
            priceDto.setDiscountRate(discountRate);
            priceDto.setRemark("学科优惠折扣：" + discountRate);
        } else {
            priceDto.setUnitPrice(unitPrice);
            priceDto.setOriginalUnitPrice(unitPrice);
        }

        return priceDto;
    }

}
